package com.example.exampleOS.slice;

    import com.example.exampleOS.MainAbility;
    import com.example.exampleOS.ResourceTable;
    import com.example.exampleOS.locator.LocationBean;
    import com.example.exampleOS.module.HomeTabList;
    import com.example.exampleOS.provider.HomePageProvider;
    import com.example.netimage.NetImage;
    import com.example.rpxutil.RpxUtil;
    import ohos.aafwk.ability.AbilitySlice;
    import ohos.aafwk.ability.continuation.*;
    import ohos.aafwk.content.Intent;
    import ohos.aafwk.content.Operation;
    import ohos.agp.colors.RgbColor;
    import ohos.agp.components.*;
    import ohos.agp.components.element.ShapeElement;
    import ohos.agp.utils.Color;
    import ohos.agp.window.dialog.ToastDialog;
    import ohos.hiviewdfx.HiLog;
    import ohos.hiviewdfx.HiLogLabel;
    import java.util.ArrayList;
    import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private int indexPage = 0;
    private PageSlider pageSlider = null;
    private TabList tabList;
//    定位start
private static final String TAG = MainAbility.class.getSimpleName();

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);

    private static final int EVENT_ID = 0x12;

    private Intent intent;
    //    定位 end

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        this.intent = intent;
        initTabList();
        initPageSlider();

    }
    private TableLayout createTableLayout(Integer columnCount,Integer rowCount){
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setColumnCount(columnCount);
        tableLayout.setRowCount(rowCount);
        tableLayout.setWidth(MagicLayout.LayoutConfig.MATCH_PARENT);
        tableLayout.setHeight(MagicLayout.LayoutConfig.MATCH_PARENT);
        tableLayout.setAlignmentType(TableLayout.ALIGN_CONTENTS);
        int val = RpxUtil.rpx(30,this);
        tableLayout.setPadding(val,val,val,val);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(255,255,255));
        tableLayout.setBackground(shapeElement);
        tableLayout.setOrientation(TableLayout.HORIZONTAL);
        return tableLayout;
    };
    private TableLayout initUI(){
        TableLayout tableLayout = createTableLayout(2, 2);

        ShapeElement buttonBg = new ShapeElement();
        buttonBg.setRgbColor(new RgbColor(0, 125, 255));
        Button netImage = createButton("网络图片", buttonBg);
        Button webView = createButton("webView", buttonBg);
        Button amap = createButton("高德地图", buttonBg);
//        webView.setMarginRight(RpxUtil.rpx(100,this));
        webView.setMarginBottom(RpxUtil.rpx(30,this));
        netImage.setMarginBottom(RpxUtil.rpx(30,this));
        netImage.setMarginRight(RpxUtil.rpx(30,this));

        amap.setMarginBottom(RpxUtil.rpx(30,this));
        amap.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new AmapSlice(),intent);
            }
        });
        netImage.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                    present(new NetImageSlice(),intent);
            }
        });
        webView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 指定待启动FA的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.example.exampleOS")
                        .withAbilityName("com.example.exampleOS.WebView")
                        .build();
                intent.setOperation(operation);
                startAbility(intent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
            }
        });
        tableLayout.addComponent(netImage);
        tableLayout.addComponent(webView);
        tableLayout.addComponent(amap);
        return tableLayout;
    }
     private TableLayout initSystem(){
        TableLayout tableLayout = createTableLayout(2, 2);

        ShapeElement buttonBg = new ShapeElement();
        buttonBg.setRgbColor(new RgbColor(0, 125, 255));
        Button dingwei = createButton("定位", buttonBg);
        Button  multiterminalSynergy= createButton("多端协同", buttonBg);
//        webView.setMarginRight(RpxUtil.rpx(100,this));
         multiterminalSynergy.setMarginBottom(RpxUtil.rpx(30,this));
         dingwei.setMarginBottom(RpxUtil.rpx(30,this));
//        dingwei.setMarginRight(RpxUtil.rpx(100,this));
         dingwei.setMarginRight(RpxUtil.rpx(30,this));
         dingwei.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new LocationSlice(),intent);
            }
        });
         multiterminalSynergy.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 指定待启动FA的bundleName和abilityName
                present(new MultiterminalSynergySlice(),intent);
            }
        });
        tableLayout.addComponent(dingwei);
        tableLayout.addComponent(multiterminalSynergy);
        return tableLayout;
    }
//定位 end
    private void initPageSlider() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setProvider(new HomePageProvider(getData(), this));
        pageSlider.setCurrentPage(indexPage);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosPixles) {
                //在选择新页面时回调。
//
            }
            @Override
            public void onPageSlideStateChanged(int state) {
                //：当页面幻灯片状态更改时调用。
            }
            @Override
            public void onPageChosen(int itemPos) {
                //当页面滑动时调用。
                tabList.selectTabAt(itemPos);
            }
        });
    }
    private ArrayList<Component> getData() {
        ArrayList<Component> dataItems = new ArrayList<>();
        dataItems.add(initUI());
        dataItems.add(initSystem());

        return dataItems;
    }
    private void initTabList(){
        HomeTabList homeTabList = new HomeTabList();
         tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        List<String> homeTabList1 = homeTabList.getHomeTabList();
        ArrayList<TabList.Tab> tabs = new ArrayList<>();//不知道什么时候用 先保存
        for (int i=0;i<homeTabList1.size();i++){
            String s = homeTabList1.get(i);
            TabList.Tab item = tabList.new Tab(getContext());
            item.setText(s);
            if(i==0){
                tabList.addTab(item,i,true);
            }else {
                tabList.addTab(item,i,false);
            }
            tabs.add(item);

        }
        tabList.setFixedMode(false);
//        int selectedTabIndex = tabList.getSelectedTabIndex();
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                System.out.println(tab.getText());
                indexPage = tab.getPosition();
                if(pageSlider!=null){
                    pageSlider.setCurrentPage(indexPage);
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
                System.out.println(tab.getText());
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
                System.out.println(tab.getText());
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    @Override
    protected void onStop() {
        super.onStop();

    }

    //    设备迁移 start
    private void showMessage(String msg) {
        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setText(msg);
        toastDialog.show();
    }
    private Button createButton(String text, ShapeElement buttonBg) {
        Button button = new Button(this);
        // 建议开发者按照自己的界面进行按钮设计,示例代码仅供参考
        final int TEXT_SIZE = 50;
        button.setTextSize(TEXT_SIZE);
        button.setTextColor(Color.YELLOW);
        button.setText(text);
        button.setWidth(RpxUtil.rpx(336,this));
        button.setHeight(RpxUtil.rpx(215,this));
        button.setBackground(buttonBg);
        return button;
    }

}
